import { _decorator, Component, Node } from 'cc';
import { ECPosition, ECPositionHelper } from '../ecs/ext/components/ECPosition';
import { TestSystem } from './TestSystem';
import { Entity } from '../ecs/core/Entity';
import { EntityCreatedEvent } from '../ecs/core/Events';
import { World } from '../ecs/core/World';

const { ccclass, property } = _decorator;

enum TestGroup {
    Group1 = 1
}

@ccclass('EcsTest')
export class EcsTest extends Component {
    world: World|undefined;
    start() {
        this.world = new World();
        this.world.addSystem(TestSystem);

        // 添加组
        this.world.addGroup(TestGroup.Group1, (entity:Entity)=>{return entity.hasComponent(ECPosition);});

        this.world.start();

        // 监听事件
        this.world.on(EntityCreatedEvent, (event:EntityCreatedEvent)=>{
            console.log("EntityAdded", event.entity.id);
        });

        
        const entity = this.world.addEntity((entity) => {
            ECPositionHelper.setPosition(entity, 100, 100, 0);
        });

        const entity2 = this.world.addEntity((entity) => {
            ECPositionHelper.setPosition(entity, 200, 200, 0);
        });

        const entity3 = this.world.addEntity((entity) => {
            ECPositionHelper.setPosition(entity, 300, 300, 0);
        });
        

        let g1 = this.world.getGroup(TestGroup.Group1);
        if (g1) {
            g1.forEach(entity=>{
                console.log(entity.id);
            });
        }
    }

    update(deltaTime: number) {
        this.world!.update(deltaTime);
    }
}


